<template>
  <a @click="visible = true">
    <span>查看历史</span>
    <a-modal v-model="visible" title="历史记录" @ok="handleOk" :width="1000">
      <a-table rowKey="id" :columns="historycolumn" :data-source="historyData" :pagination="pagination"></a-table>
    </a-modal>
  </a>
</template>
<script>
import { getHistory } from '@/api/common'
import { busHistory } from '@/dict'
import moment from 'moment'
const historycolumn = [
  { dataIndex: 'module', key: 'module', title: '修改模块' },
  { dataIndex: 'field', key: 'field', title: '字段名称' },
  { dataIndex: 'updateTime', key: 'updateTime', title: '修改时间' },
  { dataIndex: 'sysUser', key: 'sysUser', title: '修改人' },
  { dataIndex: 'fieldValue', key: 'fieldValue', title: '修改后' },
  { dataIndex: 'fieldOldValue', key: 'fieldOldValue', title: '修改前' },
  { dataIndex: 'changeType', key: 'changeType', title: '备注' }
]
export default {
  name: 'DelayHistory',
  props: ['record'],
  data() {
    return {
      visible: false,
      historycolumn,
      historyData: [],
      Type: ['新增', '修改', '删除'],
      pagination: {
        total: 1,
        pageSize: 5,
        onChange: this.getData
      }
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.getData()
      }
    }
  },
  methods: {
    handleOk() {
      this.visible = false
    },
    async getData(page = 1) {
      const form = {
        page,
        page_size: this.pagination.pageSize,
        recordType: 3,
        tableId: this.record.id
      }
      const res = await getHistory(form)
      const data = res.result.data
      this.pagination.total = data.total
      this.historyData = data.list
      this.historyData.forEach(el => {
        const tableName = el.tableName
        const fieldName = el.fieldName
        el.module = busHistory[tableName][fieldName]?.module
        el.field = busHistory[tableName][fieldName]?.field
        el.changeType = this.Type[el.changeType]
        el.updateTime = moment(el.updateTime * 1000).format('YYYY-MM-DD HH:mm:ss')
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
